package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.TbLogin;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("login")
public class LoginController {

    @RequestMapping("toLogin")
    public String toLigon(){
        return "login";
    }

    @RequestMapping("denglu")
    @ResponseBody
    public AjaxResult denglu(TbLogin tbLogin){
        //创建subject对象
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户的账号密码 token对象
        UsernamePasswordToken token = new UsernamePasswordToken(tbLogin.getPname(),tbLogin.getPwd());
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return AjaxResult.response();
        }catch (IncorrectCredentialsException e1){
            return AjaxResult.password();
        }
        return AjaxResult.ok();
    }
}
